package cn.slipi.pan.common.util;

import cn.slipi.pan.common.constants.ExceptionConstants;
import cn.slipi.pan.common.exception.ServiceException;
import org.apache.commons.lang3.StringUtils;

import java.util.Collection;
import java.util.Map;
import java.util.Set;

/**
 * 参数校验工具
 */
public class ParamCheckUtil {

    public static void checkNotNull(Object obj, String name) {
        if (null == obj) {
            throw ServiceException.create(ExceptionConstants.PARAMS_NOT_NULL, name);
        }
    }

    public static void checkNotEmpty(String str, String name) {
        if (StringUtils.isEmpty(str)) {
            throw ServiceException.create(ExceptionConstants.PARAMS_NOT_EMPTY, name);
        }
    }

    public static void checkNotEmpty(Collection collection, String name) {
        if (null == collection || collection.isEmpty()) {
            throw ServiceException.create(ExceptionConstants.PARAMS_NOT_EMPTY, name);
        }
    }

    public static void checkNotEmpty(Map map, String name) {
        if (null == map || map.isEmpty()) {
            throw ServiceException.create(ExceptionConstants.PARAMS_NOT_EMPTY, name);
        }
    }

    public static void checkNotEmpty(Set set, String name) {
        if (null == set || set.isEmpty()) {
            throw ServiceException.create(ExceptionConstants.PARAMS_NOT_EMPTY, name);
        }
    }

    public static void checkMustNull(Object obj, ExceptionConstants constant, Object... params) {
        if (null != obj) {
            throw ServiceException.create(constant, params);
        }
    }


    public static void checkNotNull(Object obj, ExceptionConstants constant, Object... params) {
        if (null == obj) {
            throw ServiceException.create(constant, params);
        }
    }

    public static void checkNotEmpty(String str, ExceptionConstants constant, Object... params) {
        if (StringUtils.isEmpty(str)) {
            throw ServiceException.create(constant, params);
        }
    }

    public static void checkNotEmpty(Collection collection, ExceptionConstants constant, Object... params) {
        if (null == collection || collection.isEmpty()) {
            throw ServiceException.create(constant, params);
        }
    }

    public static void checkNotEmpty(Map map, ExceptionConstants constant, Object... params) {
        if (null == map || map.isEmpty()) {
            throw ServiceException.create(constant, params);
        }
    }

    public static void checkNotEmpty(Set set, ExceptionConstants constant, Object... params) {
        if (null == set || set.isEmpty()) {
            throw ServiceException.create(constant, params);
        }
    }

    public static void checkExpression(boolean expression, ExceptionConstants constant, Object... params) {
        if (!expression) {
            throw ServiceException.create(constant, params);
        }
    }

    /**
     * check max length , if the string length exceeded ,an exception is thrown
     * if the string is empty , it's not checked
     * 检查最大长度,超出长度,则抛出异常 . 如果字符串本身为空,则不进行检查
     *
     * @param str       String
     * @param maxLength Maximum length
     * @param params    Exception parameters
     * @throws {@link ServiceException}
     */
    public static void checkMaxLength(String str, int maxLength, Object... params) {
        if (!StringUtils.isEmpty(str) && maxLength < str.length()) {
            throw ServiceException.create(ExceptionConstants.FILED_LENGTH_OUT, params);
        }
    }

}
